package com.yf1706.service;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yf1706.bean.Emp;
import com.yf1706.bean.SocialSecurity;
import com.yf1706.bean.SocialSecuritySob;
import com.yf1706.bean.SocialSecurityType;
import com.yf1706.dao.jpa.IEmpJDAO;
import com.yf1706.dao.jpa.SocialSecurityJDAO;
import com.yf1706.dao.jpa.SocialSecuritySobJDAO;
import com.yf1706.dao.jpa.SocialSecurityTypeJDAO;
import com.yf1706.vo.EmpSSVO;
import com.yf1706.vo.SocialSecurityVO;

/**
 * 社保服务
 * 
 * @author 锦涛
 *
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SocialSecurityService {

	@Autowired
	private SocialSecurityJDAO SecurityJDAO;

	@Autowired
	private SocialSecuritySobJDAO SecuritySobJDAO;

	@Autowired
	private SocialSecurityTypeJDAO securityTypeJDAO;

	@Autowired
	private IEmpJDAO empJDAO;

	// --------------------员工-------------------------
	/**
	 * 根据ID查找员工
	 */
	public Emp findEmpByID(int id) {
		return empJDAO.findByEmpId(id);
	}

	/**
	 * 根据部门ID查找员工
	 */
	public List<EmpSSVO> findEmpByDepID(int did) {
		return empJDAO.findEmpByDepID(did);
	}

	// --------------------社保类型-------------------------
	/**
	 * 新增社保类型
	 */
	@Transactional
	public void saveType(SocialSecurityType securityType) {
		securityTypeJDAO.save(securityType);
	}

	/**
	 * 删除社保类型
	 */
	@Transactional
	public void removeType(String ids) {
		System.out.println(ids);
		for (String id : ids.split(",")) {
			if (id != null) {
				securityTypeJDAO.deleteById(Integer.parseInt(id));
			}
		}
	}

	/**
	 * 修改社保类型
	 */
	@Transactional
	public void updateType(SocialSecurityType securityType) {
		Optional<SocialSecurityType> SsecurityType = securityTypeJDAO.findById(securityType.getSocialSecurityTypeId());
		SsecurityType.get().setSocialSecurityTypeName(securityType.getSocialSecurityTypeName());
		SsecurityType.get().setCompanyAccumulationFundRate(securityType.getCompanyAccumulationFundRate());
		SsecurityType.get().setCompanyChildbirthInsuranceRate(securityType.getCompanyChildbirthInsuranceRate());
		SsecurityType.get()
				.setCompanyEmploymentInjuryInsuranceRate(securityType.getCompanyEmploymentInjuryInsuranceRate());
		SsecurityType.get().setCompanyEndowmentInsuranceRate(securityType.getCompanyEndowmentInsuranceRate());
		SsecurityType.get().setCompanyMedicalInsuranceRate(securityType.getCompanyMedicalInsuranceRate());
		SsecurityType.get().setCompanyUnemploymentInsuranceRate(securityType.getCompanyUnemploymentInsuranceRate());
		SsecurityType.get().setPersonAccumulationFundRate(securityType.getPersonAccumulationFundRate());
		SsecurityType.get().setPersonChildbirthInsuranceRate(securityType.getPersonChildbirthInsuranceRate());
		SsecurityType.get()
				.setPersonEmploymentInjuryInsuranceRate(securityType.getPersonEmploymentInjuryInsuranceRate());
		SsecurityType.get().setPersonEndowmentInsuranceRate(securityType.getPersonEndowmentInsuranceRate());
		SsecurityType.get().setPersonMedicalInsuranceRate(securityType.getPersonMedicalInsuranceRate());
		SsecurityType.get().setPersonUnemploymentInsuranceRate(securityType.getPersonUnemploymentInsuranceRate());
	}

	/**
	 * 分页查询社保类型
	 */
	@Transactional
	public Page<SocialSecurityType> queryTypeByPage(int page, int rows) {
		Pageable pageable = PageRequest.of(page - 1, rows);
		return securityTypeJDAO.findAll(pageable);

	}

	/**
	 * 查询所有的社保类型
	 */
	public List<SocialSecurityType> queryTypeAll() {
		return securityTypeJDAO.findAll();
	}

	/**
	 * 根据ID查找社保类型
	 */
	public SocialSecurityType findTypeByID(int id) {
		return securityTypeJDAO.getOne(id);
	}

	/**
	 * 计算公司应缴纳社保总额
	 */
	public Double countCocialCompanyValue(SocialSecurityType securityType, int publicReserveFunds,
			int socialSecurityBaseNumber) {
		System.out.println("计算公司应缴纳社保总额");
		Double CompanyFundsValueRate = securityType.getCompanyAccumulationFundRate();
		Double CompanySocialValueRate = securityType.getCompanyChildbirthInsuranceRate()
				+ securityType.getCompanyEmploymentInjuryInsuranceRate()
				+ securityType.getCompanyEndowmentInsuranceRate() + securityType.getCompanyMedicalInsuranceRate()
				+ securityType.getCompanyUnemploymentInsuranceRate();
		Double socialCompanyValue = CompanyFundsValueRate * publicReserveFunds
				+ CompanySocialValueRate * socialSecurityBaseNumber;
		DecimalFormat df = new DecimalFormat("#.00");
		String a = df.format(socialCompanyValue);
		return Double.valueOf(a);
	}

	/**
	 * 计算个人应缴纳社保总额
	 */
	public Double countsocialPersonValue(SocialSecurityType securityType, int publicReserveFunds,
			int socialSecurityBaseNumber) {
		System.out.println("计算个人应缴纳社保总额");
		Double PersonFundsValueRate = securityType.getPersonAccumulationFundRate();
		Double PersonSocialValueRate = securityType.getPersonChildbirthInsuranceRate()
				+ securityType.getPersonEmploymentInjuryInsuranceRate() + securityType.getPersonEndowmentInsuranceRate()
				+ securityType.getPersonMedicalInsuranceRate() + securityType.getPersonUnemploymentInsuranceRate();
		Double socialPersonValue = PersonFundsValueRate * publicReserveFunds
				+ PersonSocialValueRate * socialSecurityBaseNumber;
		DecimalFormat df = new DecimalFormat("#.00");
		String a = df.format(socialPersonValue);
		return Double.valueOf(a);
	}

	// --------------------社保账套-------------------------
	/**
	 * 新增社保账套
	 */
	@Transactional
	public void saveSob(SocialSecuritySob securitySob) {
		SecuritySobJDAO.save(securitySob);
	}

	/**
	 * 删除社保账套
	 */
	@Transactional
	public void removeSob(String ids) {
		for (String id : ids.split(",")) {
			if (id != null) {
				SecuritySobJDAO.deleteById(Integer.parseInt(id));
			}
		}
	}

	/**
	 * 修改社保账套
	 */
	@Transactional
	public void updateSob(SocialSecuritySob securitySob) {
		Optional<SocialSecuritySob> SsecuritySob = SecuritySobJDAO.findById(securitySob.getSocialSecuritySobId());
		SsecuritySob.get().setEmpNum(securitySob.getEmpNum());
		SsecuritySob.get().setRemark(securitySob.getRemark());
		SsecuritySob.get().setSocialSecuritySobName(securitySob.getSocialSecuritySobName());
	}

	/**
	 * 分页查询社保账套
	 */
	@Transactional
	public Page<SocialSecuritySob> querySobByPage(int page, int rows, String sort, String order) {
		if (order.equals("desc")) {
			Pageable pageable = PageRequest.of(page - 1, rows, Sort.Direction.DESC, sort);
			return SecuritySobJDAO.findAll(pageable);
		} else {
			Pageable pageable = PageRequest.of(page - 1, rows, Sort.Direction.ASC, sort);
			return SecuritySobJDAO.findAll(pageable);
		}
	}

	/**
	 * 模糊分页查询本薪账套
	 */
	@Transactional
	public Page<SocialSecuritySob> findSearch(String socialSecuritySobName,int page,int rows,String sort,String order) {
		if (order.equals("desc")) {
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.DESC, sort);
			return SecuritySobJDAO.findSearch(socialSecuritySobName, pageable);
		}else{
			Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.ASC, sort);
			return SecuritySobJDAO.findSearch(socialSecuritySobName, pageable);
		}
	}
	
	
	/**
	 * 查询所有的社保账套
	 */
	public List<SocialSecuritySob> querySobAll() {
		return SecuritySobJDAO.findAll();
	}

	/**
	 * 根据ID查找社保账套
	 */
	public SocialSecuritySob findSobByID(int id) {
		return SecuritySobJDAO.getOne(id);
	}

	// --------------------社保表---------------------------
	/**
	 * 新增社保表
	 */
	@Transactional
	public void saveSS(SocialSecurity socialSecurity) {
		SecurityJDAO.save(socialSecurity);
		Optional<SocialSecuritySob> SsecuritySob = SecuritySobJDAO
				.findById(socialSecurity.getSocialSecuritySob().getSocialSecuritySobId());
		SsecuritySob.get().setEmpNum(SsecuritySob.get().getEmpNum() + 1);
	}

	/**
	 * 删除社保表
	 */
	@Transactional
	public void removeSS(String ids) {
		for (String id : ids.split(",")) {
			if (id != null) {
				SecurityJDAO.deleteById(Integer.parseInt(id));
				Optional<SocialSecuritySob> SsecuritySob = SecuritySobJDAO.findById(
						SecurityJDAO.getOne(Integer.parseInt(id)).getSocialSecuritySob().getSocialSecuritySobId());
				SsecuritySob.get().setEmpNum(SsecuritySob.get().getEmpNum() - 1);
			}
		}
	}

	/**
	 * 修改社保表
	 */
	@Transactional
	public void updateSS(SocialSecurity socialSecurity) {
		Optional<SocialSecurity> SsocialSecurity = SecurityJDAO.findById(socialSecurity.getSocialSecurityId());
		SsocialSecurity.get().setPublicReserveFunds(socialSecurity.getPublicReserveFunds());
		SsocialSecurity.get().setSocialSecurityBaseNumber(socialSecurity.getSocialSecurityBaseNumber());
		SsocialSecurity.get().setSocialSecurityType(socialSecurity.getSocialSecurityType());
		SsocialSecurity.get().setSocialSecurityCompanyValue(socialSecurity.getSocialSecurityCompanyValue());
		SsocialSecurity.get().setSocialSecurityPersonValue(socialSecurity.getSocialSecurityPersonValue());
		SsocialSecurity.get().setRemark(socialSecurity.getRemark());
		SsocialSecurity.get().setBeginDate(socialSecurity.getBeginDate());
		SsocialSecurity.get().setEndDate(socialSecurity.getEndDate());
	}

	/**
	 * 根据账套ID分页查询社保表
	 */
	@Transactional
	public Page<SocialSecurityVO> findSSByPage(int id, int page, int rows, String sort, String order) {
		if (order.equals("desc")) {
			Pageable pageable = PageRequest.of(page - 1, rows, Sort.Direction.DESC, sort);
			return SecurityJDAO.findSearch(id, pageable);
		} else {
			Pageable pageable = PageRequest.of(page - 1, rows, Sort.Direction.ASC, sort);
			return SecurityJDAO.findSearch(id, pageable);
		}
	}
	
}
